package views;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import objects.Concept;
import objects.ConceptList;

import javax.swing.JButton;
import javax.swing.JComboBox;

public class ViewOntology extends JFrame{
	
	/* Data variables */
	private ConceptList currentList;
	
	/** Component variables **/
	
	private JPanel contentPane;
	
	private JTable table;
	private DefaultTableModel tableModel;
	
	private JComboBox<String> cmbPOS;
	
	private JButton btnGo;
	
	public ViewOntology(){
		currentList = null;
		
		setTitle("Ontology");
		setMinimumSize(new Dimension(300, 200));
		setSize(new Dimension(500, 400));
		setLocationRelativeTo(null);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panTop = new JPanel();
		contentPane.add(panTop, BorderLayout.NORTH);
		panTop.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		cmbPOS = new JComboBox<>();
		panTop.add(cmbPOS);
		
		btnGo = new JButton("Go");
		panTop.add(btnGo);
		
		JScrollPane scpCenter = new JScrollPane();
		contentPane.add(scpCenter, BorderLayout.CENTER);

		String[] baseColumns = new String[] {"Concept Stems", "Senses", "English Glosses"};
		
		tableModel = new DefaultTableModel(new Object[][] {}, baseColumns);
		
		table = new JTable(tableModel) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		scpCenter.setViewportView(table);
		
		TableColumnAdjuster adjuster = new TableColumnAdjuster(table);
		adjuster.setDynamicAdjustment(true);
		adjuster.adjustColumns();
	}
	
	public void loadOntology(ConceptList conceptList) {
		currentList = conceptList;
		
		clearTable();
		addRows(conceptList);
	}
	
	/** Table methods **/
	
	public void addRows(ConceptList concepts) {
		addRows(concepts.getConceptList());
	}
	
	public void addRows(ArrayList<Concept> concepts) {
		for (Concept concept : concepts) {
			addRow(concept);
		}
	}
	
	public void addRow(Concept concept) {
		tableModel.addRow(new String[] {concept.getName(), concept.getSense(), concept.getDefinition()});
	}
	
	public void clearTable() {
		while (tableModel.getRowCount() > 0) {
			tableModel.removeRow(0);
		}
	}
	
	public Concept getSelectedConcept() {
		int selected = table.getSelectedRow();
		Concept concept = currentList.getConceptList().get(selected);
		
		return concept;
	}
	
	/** ComboBox methods **/
	
	public void setLabelsForChooser(ArrayList<ConceptList> concepts) {
		ArrayList<String> labels = new ArrayList<>();
		
		for (ConceptList concept : concepts) {
			labels.add(concept.getLabel());
		}
		
		addLabelsForChooser(labels);
	}
	
	public void addLabelsForChooser(ArrayList<String> labels) {
		for (String label : labels) {
			cmbPOS.addItem(label);
		}
	}
	
	public int getSelectedIndex() {
		return cmbPOS.getSelectedIndex();
	}
	
	/** Add listeners **/
	
	public void addNavListener(ActionListener actionListener) {
		btnGo.addActionListener(actionListener);
	}
	
	public void addTableListener(MouseListener mouseListener) {
		table.addMouseListener(mouseListener);
	}
	
	/** Get data **/
	
	public ConceptList getCurrentList() {
		return currentList;
	}
}
